<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * HITI AdminCP
 *
 * @package     HITI AdminCP Project
 * @since       Monday, October 21, 2013, 6:00 PM
 * @final       
 * @category    Models
 * @see 		Regions
 * @author      PHUOCNGUYEN - HITI Dev Team
 * @copyright   Copyright (c) 2013, HITI Corp
 */

class Regions_model extends CI_Model {
	public function __construct()
	{
		parent::__construct();
	}

	public function show($q = '')
	{
		$this->db->query('SET NAMES "LATIN1"');
		if ($q != '')
		{
			$this->db->like('name_vn', $q);
			$this->db->or_like('keyword', str_replace(' ', '-', $q));
			$this->db->select('id, parent');
			$query = $this->db->get('regions');
			foreach ($query->result_array() as $row)
			{
				$ids[] = $row['parent'];
				$ids[] = $row['id'];
			}
			$ids = $this->search_reg($ids);
			$this->db->where_in('id', $ids);
		}
		$last_login = $GLOBALS['user']['last_login'];
		$this->db->from('regions AS r');
		$this->db->select('r.id, r.parent, r.name_vn, r.deleted, r.active, r.price, r.cat, count(s.parent) AS chirld, IF ('.sprintf('r.date_added >= %1$s', $this->db->escape($last_login)).', 1, 0) as isnew', false);
		$this->db->group_by('id');
		$this->db->join('regions as s', 'r.id = s.parent', 'left');
		$this->db->order_by('r.sort_order DESC');
		$query = $this->db->get();
		if ($query->num_rows() > 0)
		{
			return $query->result_array();
		}
		else
		{
			return false;
		}
	}

	private function search_reg($ids)
	{
		$break = false;
		$this->db->where_in('id', $ids);
		$this->db->select('parent');
		$query = $this->db->get('regions');
		if ($query->num_rows() > 0)
		{
			foreach ($query->result_array() as $row)
			{
				$ids[] = $row['parent'];
				if ($row['parent'] == 0) $break = true;
			}
			if (!$break) $ids = $this->search_reg($ids);
		}
		return $ids;
	}

	public function info($id)
	{
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->where('id', $id);
		$query = $this->db->get('regions');
		if ($query->num_rows() > 0)
		{
			return $query->row_array();
		}
		else
		{
			$fields = $this->db->field_data('regions');
			$default_data = array();
			foreach ($fields as $field)
			{
				$default_data[$field->name] = $field->default;
			}
			return $default_data;
		}
	}

	public function check($name, $parent = '', $id = '')
	{
		if ($parent == '') $parent = 0;
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->where('name_vn', $name);
		if ($id > 0) $this->db->where('id <>', $id);
		$this->db->where('parent', $parent);
		if ($this->db->get('regions')->num_rows() > 0)
		{
			return 1;
		}
		else
		{
			return 0;
		}
	}


	public function process($data, $id = '')
	{
		if(!is_array($data))
		{
			return false;
		}
		$this->db->query('SET NAMES "LATIN1"');	
		if ($id > 0)
		{
			unset($data['date_added']);
			$this->db->where('id', $id);
			$this->db->update('regions', $data);
			write_log('update_regions', $data['name_vn']);
		}
		else
		{
			$this->db->insert('regions', $data);
			write_log('add_regions', $data['name_vn']);
		}
		return true;
	}
}

/* End of file regions_model.php */
/* Location: ./application/models/regions_model.php */